import axios from 'axios'
class Tools {
  /**
   * 本地localStorage数据存储或读取
   * @param {string} key
   * @param {string} value
   * @returns
   * @memberof Tools
   */
  localItem (key, value) {
    if (arguments.length === 1) {
      return localStorage.getItem(key)
    } else {
      return localStorage.setItem(key, value)
    }
  }
  /**
   * 删除本地localStorage数据
   * @param {string} key
   * @returns
   * @memberof Tools
   */
  removeLocalItem (key) {
    if (key) {
      return localStorage.removeItem(key)
    }
    return localStorage.removeItem()
  }
  /**
   * 本地sessionStorage数据存储或读取
   * @param {string} key
   * @param {string} value
   * @returns
   * @memberof Tools
   */
  sessionItem (key, value) {
    if (arguments.length === 1) {
      return sessionStorage.getItem(key)
    } else {
      return sessionStorage.setItem(key, value)
    }
  }
  /**
   * 删除本地sessionStorage数据
   * @param {string} key
   * @returns
   * @memberof Tools
   */
  removeSessionItem (key) {
    if (key) {
      return sessionStorage.removeItem(key)
    }
    return sessionStorage.removeItem()
  }
  /**
   * 设置cookie
   * @param {string} name
   * @param {string} value
   * @param {string} expires
   * @param {string} domain
   * @param {string} path
   * @param {string} secure
   * @memberof Tools
   */
  setCookie (name, value, expires, domain, secure) {
    let cookieText = ''
    cookieText += encodeURIComponent(name) + '=' + encodeURIComponent(value)
    if (expires instanceof Date) {
      cookieText += '; expires=' + expires.toGMTString()
    }
    // if (path) {
    cookieText += '; path=' + '/'
    // }
    if (domain) {
      cookieText += '; domain=' + domain
    }
    if (secure) {
      cookieText += '; secure'
    }
    document.cookie = cookieText
  }
  /**
   * 获取cookie
   * @param {string} name
   * @returns
   * @memberof Tools
   */
  getCookie (name) {
    let cookieName = encodeURIComponent(name) + '='
    let cookieStart = document.cookie.indexOf(cookieName)
    let cookieValue = ''
    if (cookieStart > -1) {
      let cookieEnd = document.cookie.indexOf(';', cookieStart)
      if (cookieEnd == -1) {
        cookieEnd = document.cookie.length
      }
      cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
    }
    return cookieValue
  }
  /**
   * 删除cookie
   * @param {string} name
   * @param {string} domain
   * @param {string} path
   * @param {string} secure
   * @memberof Tools
   */
  deletCookie (name, domain, secure) {
    this.setCookie(name, '', '-1', domain, secure)
  }

  /**
   * 去抖动延迟执行方法
   * @param {function} fn 回调函数
   * @param {long} delay 延迟时间
   */
  debounce (fn, delay) {
    var timeoutID = null
    return function () {
      clearTimeout(timeoutID)
      var args = arguments
      var that = this
      timeoutID = setTimeout(function () {
        fn.apply(that, args)
      }, delay)
    }
  }

  /**
   * 生成 UUID
   * @returns UUID V4
   */
  uuidv4 () {
    return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
      (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    )
  }

  // 格式化角色显示
  formatRole (role) {
    if (!role) {
      return null
    }
    role = role.toLowerCase()
    switch (role) {
      case 'collaborator':
        return i18n.global.t('loc.teacherRole')
      case 'site_admin':
        return i18n.global.t('loc.siteAdmin')
      case 'agency_admin':
        return i18n.global.t('loc.agenAdmin')
      case 'agency_owner':
        return i18n.global.t('loc.agencyOwner')
      case 'parent':
        return i18n.global.t('loc.parent')
      case 'family_service':
        return i18n.global.t('loc.familyService')
      case 'teaching_assistant':
        return i18n.global.t('loc.teachingAssistant')
    }
  }

  getDRDPRatingColor (level) {
    switch (level) {
      case 'Discovering Language':
        return '#e4e7ee'
      case 'Discovering English':
        return '#cbd0df'
      case 'Exploring English':
        return '#b5bdd3'
      case 'Developing English':
        return '#a0adc9'
      case 'Building English':
        return '#8d9dbf'
      case 'Integrating English':
        return '#7a8eb6'
      case 'Discovering Spanish':
        return '#cbd0df'
      case 'Exploring Spanish':
        return '#b5bdd3'
      case 'Developing Spanish':
        return '#a0adc9'
      case 'Building Spanish':
        return '#8d9dbf'
      case 'Integrating Spanish':
        return '#7a8eb6'
      case 'Integrating Earlier':
        return '#e7e1ef'
      case 'Integrating Middle':
        return '#ccc0dc'
      case 'Integrating Later':
        return '#b6aac7'
      case 'Building Earlier':
        return '#dce5ee'
      case 'Building Middle':
        return '#b9ccdd'
      case 'Building Later':
        return '#8baac7'
      case 'Exploring Earlier':
        return '#dceedf'
      case 'Exploring Middle':
        return '#b9dec1'
      case 'Exploring Later':
        return '#8bc797'
      case 'Responding Earlier':
        return '#f9e5bb'
      case 'Responding Later':
        return '#f6d38f'
    }
    return ''
  }

  download (url, fileName) {
    url = url.replace(/\\/g, '/')
    axios({
      method: 'get',
      url: url,
      headers: { 'Origin': '' },
      responseType: 'blob'
    }).then(data => {
      this.saveAs(data, fileName)
    }).catch(error => {
      console.log(error)
      var link = document.createElement('a')
      link.target = '_blank'
      link.href = url
      link.download = fileName
      link.click()
      link.remove()
    })
  }

  saveAs (data, name) {
    var urlObject = window.URL || window.webkitURL || window
    var blob = new Blob([data])
    var link = document.createElement('a')
    link.target = '_blank'
    link.href = urlObject.createObjectURL(blob)
    link.download = name
    link.click()
    link.remove()
  }

  // 判断日期是否为当天,传入格式为YYYY-MM-DD
  isToday (inDate) {
    let nowDate = new Date()
    let checkDate = new Date(inDate)
    return (nowDate.toDateString() === checkDate.toDateString())
  }
  isNotEmpty (str) {
    return str && str.length > 0
  }
  // 访问来源是否来自iPad
  isComeFromIPad () {
    // 判断用户终端类型
    if (/(iPad)/i.test(navigator.userAgent)) {
      return true
    }
    return false
  }
  // 计算转换文件大小
  calFilesize (size) {
    if (!size) {
      return '0B'
    }
    var num = 1024.00 // byte
    if (size < num) {
      return size + 'B'
    }
    if (size < Math.pow(num, 2)) {
      return (size / num).toFixed(2) + 'KB' // kb
    }
    if (size < Math.pow(num, 3)) {
      return (size / Math.pow(num, 2)).toFixed(2) + 'MB' // M
    }
    if (size < Math.pow(num, 4)) {
      return (size / Math.pow(num, 3)).toFixed(2) + 'GB' // G
    }
    return (size / Math.pow(num, 4)).toFixed(2) + 'TB' // T
  }
}
export default new Tools()
